#include "drv_rtt_led/drv_rtt_led.h"
#include "drivers/pin.h"

//#define DBG_LVL           DBG_INFO
#define DBG_LVL           DBG_LOG
//#define DBG_LVL           DBG_NODBG
#include <mydbg.h>          // must after of DBG_LVL, DBG_TAG or other options

/*********************************************************************************************
* 名称：led_pin_init()
* 功能：LED引脚初始化
* 参数：无
* 返回：无
* 修改：
* 注释：
*********************************************************************************************/
void led_pin_init(void)
{
#ifdef BOARD_CONFIG_RTT_LED0_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_LED0_PIN, PIN_MODE_OUTPUT);
  led_write(0,0);
#endif
#ifdef BOARD_CONFIG_RTT_LED1_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_LED1_PIN, PIN_MODE_OUTPUT);
  led_write(1,0);
#endif
#ifdef BOARD_CONFIG_RTT_LED2_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_LED2_PIN, PIN_MODE_OUTPUT);
  led_write(2,0);
#endif
#ifdef BOARD_CONFIG_RTT_LED3_PIN
  rt_pin_mode(BOARD_CONFIG_RTT_LED3_PIN, PIN_MODE_OUTPUT);
  led_write(3,0);
#endif
}

// cmd：每位控制一个LED灯，为1时LED点亮 为0熄灭
void led_ctrl(unsigned char cmd)
{
#ifdef BOARD_CONFIG_RTT_LED0_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED0_PIN, BOARD_CONFIG_RTT_LED0_ACTIVATE ? (cmd & LED0_NUM) : !(cmd & LED0_NUM));
#endif
#ifdef BOARD_CONFIG_RTT_LED1_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED1_PIN, BOARD_CONFIG_RTT_LED1_ACTIVATE ? (cmd & LED1_NUM) : !(cmd & LED1_NUM));
#endif
#ifdef BOARD_CONFIG_RTT_LED2_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED2_PIN, BOARD_CONFIG_RTT_LED2_ACTIVATE ? (cmd & LED2_NUM) : !(cmd & LED2_NUM));
#endif
#ifdef BOARD_CONFIG_RTT_LED3_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED3_PIN, BOARD_CONFIG_RTT_LED3_ACTIVATE ? (cmd & LED3_NUM) : !(cmd & LED3_NUM));
#endif
}

// index LED 索引，value：为1时LED点亮 为0熄灭
void led_write(unsigned char index ,unsigned char value)
{
  value = !!value;
  switch(index){
    case 0:
#ifdef BOARD_CONFIG_RTT_LED0_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED0_PIN, BOARD_CONFIG_RTT_LED0_ACTIVATE ? value : !value);
#endif
    break;
    
    case 1:
#ifdef BOARD_CONFIG_RTT_LED1_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED1_PIN, BOARD_CONFIG_RTT_LED1_ACTIVATE ? value : !value);
#endif
    break;
    
    case 2:
#ifdef BOARD_CONFIG_RTT_LED2_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED2_PIN, BOARD_CONFIG_RTT_LED2_ACTIVATE ? value : !value);
#endif
    break;
    
    case 3:
#ifdef BOARD_CONFIG_RTT_LED3_PIN
  rt_pin_write(BOARD_CONFIG_RTT_LED3_PIN, BOARD_CONFIG_RTT_LED3_ACTIVATE ? value : !value);
#endif
    break;
    default:
    logfn_e("index error");
    break;
  }
}

